<?php

/**
 * Created by PhpStorm.
 * User: andy
 * Date: 16/4/9
 * Time: 12:22
 */
class Domain_Lottery {
    public $db;

    public function  __construct() {
        $this->db = new Model_Lottery();

    }

    function gettotal() {
        return $this->db->gettotal();
    }

    function gettotalByToday() {
        return $this->db->gettotalByToday();
    }

    function gettotalUser($openid) {
        return $this->db->gettotalUser($openid);
    }

    function gettotalUserByTodayy($openid) {
        return $this->db->gettotalUserByToday($openid);
    }

    function insertUser($openid, $scores) {
        $time = DI()->helper->getTime();
        $ip = DI()->helper->getClientIp();
        return $this->db->insertUser($openid, $time, $ip, $scores);
    }
    function setLottery($id){

        return $this->db->setLottery($id);
    }

    function ranking($num) {

        $data = DI()->cache->zRevRange('wxvk:ranking',0,$num-1,true);
        $_data = [];
        $i = 0;
        foreach ($data as $item=>$value) {
            $_data[] = [
                "name"    => $this->getName($item),
                "scores"  => intval($value),
                "ranking" => ++$i,
            ];
        }
        return $_data;
    }
    function rankingUpdate(){
        $list=$this->db->getAllList();
        foreach($list as $item){
            $currScores = DI()->cache->zScore("wxvk:ranking",$item['openid']);
            if($item['scores']>$currScores) DI()->cache->zAdd("wxvk:ranking",$item['scores'],$item['openid']);
        }
        return true;
    }
    /**
     * 根据openid获取名字
     *
     */
    function getName($openid) {

        $name = DI()->cache->get("name:" . $openid);
        if ($name) {
            return $name;
        }
        $user = new Model_LotteryUsers();
        $userInfo = $user->userInfoByOpenid($openid);
        if ($userInfo) {
            DI()->cache->set("name:" . $openid, DI()->helper->hideStar($userInfo['name']));
            return  DI()->helper->hideStar($userInfo['name']);
        }
        return "神秘用户";



    }


//wxvk_Lottery

}